using Engine;
using Engine.Graphics;
using Game;

namespace SCIENEW {
    public class CastingTroughPipeFluidConnection : PipeFluidConnection, IDrawable {
        public PrimitivesRenderer3D m_primitivesRenderer3D = new();
        public readonly List<Point3> m_pipePoints = new();

        public CastingTroughPipeFluidConnection(HashSet<FluidTransferrer> fromTransferrers, HashSet<FluidTransferrer> toTransferrers, HashSet<FluidConnector> fromConnectors, HashSet<FluidConnector> toConnectors, float rate, Type transferringType, SubsystemFluidTransfer subsystemFluidTransfer) : base(
            fromTransferrers,
            toTransferrers,
            fromConnectors,
            toConnectors,
            rate,
            transferringType,
            subsystemFluidTransfer
        ) {
        }

        public override void Init() {
            foreach (var pipeFluidConnection in SubsystemFluidTransfer.m_pipeConnections) {
                if (pipeFluidConnection.Value == this) m_pipePoints.Add(pipeFluidConnection.Key);
            }
        }

        public void Draw(Camera camera, int drawOrder) {
            if (IsTransferring && TransferringFluid != null && TransferringFluid > 0 && m_pipePoints.Count > 0) {
                Fluid fluid = FluidsManager.GetFluid(TransferringFluid.Value);
                //判断方向
                int blockValue = SubsystemFluidTransfer.m_subsystemTerrain.Terrain.GetCellValueFast(m_pipePoints[0].X, m_pipePoints[0].Y, m_pipePoints[0].Z);
                int direction = FourDirectionModelDevice.GetDirection_Static(blockValue);
                foreach (var pipePoint in m_pipePoints) {
                    Vector3 wp1 = new Vector3(pipePoint) + Vector3.UnitY * 0.45f;
                    Vector3 wp2 = new Vector3(pipePoint) + Vector3.UnitY * 0.45f + Vector3.UnitX;
                    Vector3 wp3 = new Vector3(pipePoint) + Vector3.UnitY * 0.45f + Vector3.UnitX + Vector3.UnitZ;
                    Vector3 wp4 = new Vector3(pipePoint) + Vector3.UnitY * 0.45f + Vector3.UnitZ;
                    float texWidth = 1f / fluid.GetTextureSubdivision(TransferringFluid.Value);
                    Vector2 cd1 = Vector2.Zero;
                    Vector2 cd2 = Vector2.UnitX * texWidth;
                    Vector2 cd3 = Vector2.One * texWidth;
                    Vector2 cd4 = Vector2.UnitY * texWidth;
                    if (direction is 1 or 3) {
                        wp1 += Vector3.UnitZ * 0.25f;
                        wp2 += Vector3.UnitZ * 0.25f;
                        wp3 -= Vector3.UnitZ * 0.25f;
                        wp4 -= Vector3.UnitZ * 0.25f;
                        cd1 += Vector2.UnitY * 0.25f * texWidth;
                        cd2 += Vector2.UnitY * 0.25f * texWidth;
                        cd3 -= Vector2.UnitY * 0.25f * texWidth;
                        cd4 -= Vector2.UnitY * 0.25f * texWidth;
                    }
                    else if (direction is 0 or 2) {
                        wp1 += Vector3.UnitX * 0.25f;
                        wp2 -= Vector3.UnitX * 0.25f;
                        wp3 -= Vector3.UnitX * 0.25f;
                        wp4 += Vector3.UnitX * 0.25f;
                        cd1 += Vector2.UnitX * 0.25f * texWidth;
                        cd2 -= Vector2.UnitX * 0.25f * texWidth;
                        cd3 -= Vector2.UnitX * 0.25f * texWidth;
                        cd4 += Vector2.UnitX * 0.25f * texWidth;
                    }
                    m_primitivesRenderer3D.TexturedBatch(fluid.GetTexture(TransferringFluid.Value), samplerState: SamplerState.PointWrap).QueueQuad(wp1, wp2, wp3, wp4, cd1, cd2, cd3, cd4, fluid.GetColor(TransferringFluid.Value));
                }

            }
            m_primitivesRenderer3D.Flush(camera.ViewProjectionMatrix);
        }

        public int[] DrawOrders => [2000];
    }
}